﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GIMS.MODEL;
using KPIS.GERP.GIMS.IDAL;

namespace KPIS.GERP.GIMS.DAL
{
    public sealed class ResourceMasterSubGroupDAL : IDALResourceMasterSubGroup
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public ResourceMasterSubGroupDAL()
        {

        }

        public IList<ResourceMasterSubGroupInfo> GetResourceMasterSubGroup()
        {
            string strSQL;
            DataSet ds = null;
            DataRowCollection dr = null;
            IList<ResourceMasterSubGroupInfo> infoList = new List<ResourceMasterSubGroupInfo>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);

                strSQL = "SELECT *"
                        + " FROM ICM_MAS_RESOURCE_SUB_GROUP"
                        + " WHERE RECORD_STATUS <> @delete_status";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    for (int i = 0; i < dr.Count; i++)
                    {
                        ResourceMasterSubGroupInfo info = new ResourceMasterSubGroupInfo();
                        ResourceMasterGroupInfo infoGroup = new ResourceMasterGroupInfo();
                        RecordInfo infoRecord = new RecordInfo();
                        SysRecordStatusInfo infoStatus = new SysRecordStatusInfo();
                        info.Seq = Convert.ToInt32(dr[i]["ICM_RES_SUB_GRP_SEQ"]);
                        info.Code = (dr[i]["ICM_RES_SUB_GRP_CODE"]).ToString();
                        info.Name = (dr[i]["ICM_RES_SUB_GRP_NAME"]).ToString();
                        info.Descr = (dr[i]["ICM_RES_SUB_GRP_DESCR"]).ToString();
                        info.Unspsc_code = (dr[i]["UNSPSC_CODE"]).ToString();
                        info.Inc_code = (dr[i]["INC_CODE"]).ToString();
                        infoGroup.Seq = (Convert.IsDBNull(dr[i]["ICM_RES_GRP_SEQ"]) ? null : (Nullable<int>)(dr[i]["ICM_RES_GRP_SEQ"]));
                        infoRecord.Created_by = (Convert.IsDBNull(dr[i]["CREATED_BY"]) ? null : (Nullable<int>)(dr[i]["CREATED_BY"]));
                        infoRecord.Created_when = (Convert.IsDBNull(dr[i]["CREATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[i]["CREATED_WHEN"]));
                        infoRecord.Updated_by = (Convert.IsDBNull(dr[i]["UPDATED_BY"]) ? null : (Nullable<int>)(dr[i]["UPDATED_BY"]));
                        infoRecord.Updated_when = (Convert.IsDBNull(dr[i]["UPDATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[i]["UPDATED_WHEN"]));
                        infoRecord.Restored_by = (Convert.IsDBNull(dr[i]["RESTORED_BY"]) ? null : (Nullable<int>)(dr[i]["RESTORED_BY"]));
                        infoRecord.Restored_when = (Convert.IsDBNull(dr[i]["RESTORED_WHEN"]) ? null : (Nullable<DateTime>)(dr[i]["RESTORED_WHEN"]));
                        infoStatus.Status = (dr[i]["RECORD_STATUS"]).ToString();
                        infoRecord.InfoStatus = infoStatus;
                        info.InfoGroup = infoGroup;
                        info.InfoRecord = infoRecord;
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
                //return null;
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public IList<ResourceMasterSubGroupInfo> GetResourceMasterSubGroup(string record_status)
        {
            string strSQL;
            DataSet ds = null;
            DataRowCollection dr = null;
            IList<ResourceMasterSubGroupInfo> infoList = new List<ResourceMasterSubGroupInfo>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@record_status", record_status);

                strSQL = "SELECT *"
                        + " FROM ICM_MAS_RESOURCE_SUB_GROUP"
                        + " WHERE RECORD_STATUS = @record_status";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    for (int i = 0; i < dr.Count; i++)
                    {
                        ResourceMasterSubGroupInfo info = new ResourceMasterSubGroupInfo();
                        ResourceMasterGroupInfo infoGroup = new ResourceMasterGroupInfo();
                        RecordInfo infoRecord = new RecordInfo();
                        SysRecordStatusInfo infoStatus = new SysRecordStatusInfo();
                        info.Seq = Convert.ToInt32(dr[i]["ICM_RES_SUB_GRP_SEQ"]);
                        info.Code = (dr[i]["ICM_RES_SUB_GRP_CODE"]).ToString();
                        info.Name = (dr[i]["ICM_RES_SUB_GRP_NAME"]).ToString();
                        info.Descr = (dr[i]["ICM_RES_SUB_GRP_DESCR"]).ToString();
                        info.Unspsc_code = (dr[i]["UNSPSC_CODE"]).ToString();
                        info.Inc_code = (dr[i]["INC_CODE"]).ToString();
                        infoGroup.Seq = (Convert.IsDBNull(dr[i]["ICM_RES_GRP_SEQ"]) ? null : (Nullable<int>)(dr[i]["ICM_RES_GRP_SEQ"]));
                        infoRecord.Created_by = (Convert.IsDBNull(dr[i]["CREATED_BY"]) ? null : (Nullable<int>)(dr[i]["CREATED_BY"]));
                        infoRecord.Created_when = (Convert.IsDBNull(dr[i]["CREATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[i]["CREATED_WHEN"]));
                        infoRecord.Updated_by = (Convert.IsDBNull(dr[i]["UPDATED_BY"]) ? null : (Nullable<int>)(dr[i]["UPDATED_BY"]));
                        infoRecord.Updated_when = (Convert.IsDBNull(dr[i]["UPDATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[i]["UPDATED_WHEN"]));
                        infoRecord.Restored_by = (Convert.IsDBNull(dr[i]["RESTORED_BY"]) ? null : (Nullable<int>)(dr[i]["RESTORED_BY"]));
                        infoRecord.Restored_when = (Convert.IsDBNull(dr[i]["RESTORED_WHEN"]) ? null : (Nullable<DateTime>)(dr[i]["RESTORED_WHEN"]));
                        infoStatus.Status = (dr[i]["RECORD_STATUS"]).ToString();
                        infoRecord.InfoStatus = infoStatus;
                        info.InfoGroup = infoGroup;
                        info.InfoRecord = infoRecord;
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
                //return null;
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public ResourceMasterSubGroupInfo GetResourceMasterSubGroup(int seq)
        {
            string strSQL;
            DataSet ds;
            DataRowCollection dr;
            ResourceMasterSubGroupInfo info = new ResourceMasterSubGroupInfo();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@seq", seq);

                strSQL = "SELECT *"
                        + " FROM ICM_MAS_RESOURCE_SUB_GROUP"
                        + " WHERE RECORD_STATUS <> @delete_status"
                            + " AND ICM_RES_SUB_GRP_SEQ = @seq";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    ResourceMasterGroupInfo infoGroup = new ResourceMasterGroupInfo();
                    RecordInfo infoRecord = new RecordInfo();
                    SysRecordStatusInfo infoStatus = new SysRecordStatusInfo();
                    info.Seq = Convert.ToInt32(dr[0]["ICM_RES_SUB_GRP_SEQ"]);
                    info.Code = (dr[0]["ICM_RES_SUB_GRP_CODE"]).ToString();
                    info.Name = (dr[0]["ICM_RES_SUB_GRP_NAME"]).ToString();
                    info.Descr = (dr[0]["ICM_RES_SUB_GRP_DESCR"]).ToString();
                    info.Unspsc_code = (dr[0]["UNSPSC_CODE"]).ToString();
                    info.Inc_code = (dr[0]["INC_CODE"]).ToString();
                    infoGroup.Seq = (Convert.IsDBNull(dr[0]["ICM_RES_GRP_SEQ"]) ? null : (Nullable<int>)(dr[0]["ICM_RES_GRP_SEQ"]));
                    infoRecord.Created_by = (Convert.IsDBNull(dr[0]["CREATED_BY"]) ? null : (Nullable<int>)(dr[0]["CREATED_BY"]));
                    infoRecord.Created_when = (Convert.IsDBNull(dr[0]["CREATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[0]["CREATED_WHEN"]));
                    infoRecord.Updated_by = (Convert.IsDBNull(dr[0]["UPDATED_BY"]) ? null : (Nullable<int>)(dr[0]["UPDATED_BY"]));
                    infoRecord.Updated_when = (Convert.IsDBNull(dr[0]["UPDATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[0]["UPDATED_WHEN"]));
                    infoRecord.Restored_by = (Convert.IsDBNull(dr[0]["RESTORED_BY"]) ? null : (Nullable<int>)(dr[0]["RESTORED_BY"]));
                    infoRecord.Restored_when = (Convert.IsDBNull(dr[0]["RESTORED_WHEN"]) ? null : (Nullable<DateTime>)(dr[0]["RESTORED_WHEN"]));
                    infoStatus.Status = (dr[0]["RECORD_STATUS"]).ToString();
                    infoRecord.InfoStatus = infoStatus;
                    info.InfoGroup = infoGroup;
                    info.InfoRecord = infoRecord;
                }
            }
            catch (Exception ex)
            {
                throw (ex);
                //return null;
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }

        public IList<ResourceMasterSubGroupInfo> GetResourceMasterSubGroupWithParent(int group_seq)
        {
            string strSQL;
            DataSet ds = null;
            DataRowCollection dr = null;
            IList<ResourceMasterSubGroupInfo> infoList = new List<ResourceMasterSubGroupInfo>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@group_seq", group_seq);

                strSQL = "SELECT *"
                        + " FROM ICM_MAS_RESOURCE_SUB_GROUP"
                        + " WHERE RECORD_STATUS <> @delete_status"
                            + " AND ICM_RES_GRP_SEQ = @group_seq";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    for (int i = 0; i < dr.Count; i++)
                    {
                        ResourceMasterSubGroupInfo info = new ResourceMasterSubGroupInfo();
                        ResourceMasterGroupInfo infoGroup = new ResourceMasterGroupInfo();
                        RecordInfo infoRecord = new RecordInfo();
                        SysRecordStatusInfo infoStatus = new SysRecordStatusInfo();
                        info.Seq = Convert.ToInt32(dr[i]["ICM_RES_SUB_GRP_SEQ"]);
                        info.Code = (dr[i]["ICM_RES_SUB_GRP_CODE"]).ToString();
                        info.Name = (dr[i]["ICM_RES_SUB_GRP_NAME"]).ToString();
                        info.Descr = (dr[i]["ICM_RES_SUB_GRP_DESCR"]).ToString();
                        info.Unspsc_code = (dr[i]["UNSPSC_CODE"]).ToString();
                        info.Inc_code = (dr[i]["INC_CODE"]).ToString();
                        infoGroup.Seq = (Convert.IsDBNull(dr[i]["ICM_RES_GRP_SEQ"]) ? null : (Nullable<int>)(dr[i]["ICM_RES_GRP_SEQ"]));
                        infoRecord.Created_by = (Convert.IsDBNull(dr[i]["CREATED_BY"]) ? null : (Nullable<int>)(dr[i]["CREATED_BY"]));
                        infoRecord.Created_when = (Convert.IsDBNull(dr[i]["CREATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[i]["CREATED_WHEN"]));
                        infoRecord.Updated_by = (Convert.IsDBNull(dr[i]["UPDATED_BY"]) ? null : (Nullable<int>)(dr[i]["UPDATED_BY"]));
                        infoRecord.Updated_when = (Convert.IsDBNull(dr[i]["UPDATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[i]["UPDATED_WHEN"]));
                        infoRecord.Restored_by = (Convert.IsDBNull(dr[i]["RESTORED_BY"]) ? null : (Nullable<int>)(dr[i]["RESTORED_BY"]));
                        infoRecord.Restored_when = (Convert.IsDBNull(dr[i]["RESTORED_WHEN"]) ? null : (Nullable<DateTime>)(dr[i]["RESTORED_WHEN"]));
                        infoStatus.Status = (dr[i]["RECORD_STATUS"]).ToString();
                        infoRecord.InfoStatus = infoStatus;
                        info.InfoGroup = infoGroup;
                        info.InfoRecord = infoRecord;
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
                //return null;
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public void Insert(ResourceMasterSubGroupInfo info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(11);

                dbManager.AddParameters(0, "@code", info.Code);
                dbManager.AddParameters(1, "@name", info.Name);
                dbManager.AddParameters(2, "@descr", info.Descr);
                dbManager.AddParameters(3, "@unspsc_code", info.Unspsc_code);
                dbManager.AddParameters(4, "@inc_code", info.Inc_code);
                dbManager.AddParameters(5, "@group_seq", info.InfoGroup.Seq);
                dbManager.AddParameters(6, "@record_status", SystemInfo._WorkRecordStatus);
                dbManager.AddParameters(7, "@created_by", info.InfoRecord.Created_by);
                dbManager.AddParameters(8, "@created_when", info.InfoRecord.Created_when);
                dbManager.AddParameters(9, "@updated_by", info.InfoRecord.Updated_by);
                dbManager.AddParameters(10, "@updated_when", info.InfoRecord.Updated_when);

                strSQL = "INSERT INTO ICM_MAS_RESOURCE_SUB_GROUP"
                    + " (ICM_RES_SUB_GRP_CODE, ICM_RES_SUB_GRP_NAME, ICM_RES_SUB_GRP_DESCR, UNSPSC_CODE, INC_CODE, ICM_RES_GRP_SEQ"
                    + " , RECORD_STATUS, CREATED_BY, CREATED_WHEN, UPDATED_BY, UPDATED_WHEN)"
                    + " VALUES(@code"
                    + ", @name"
                    + ", @descr"
                    + ", @unspsc_code"
                    + ", @inc_code"
                    + ", @group_seq"
                    + ", @record_status"
                    + ", @created_by"
                    + ", @created_when"
                    + ", @updated_by"
                    + ", @updated_when"
                    + " )";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update(ResourceMasterSubGroupInfo info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(12);

                dbManager.AddParameters(0, "@seq", info.Seq);
                dbManager.AddParameters(1, "@code", info.Code);
                dbManager.AddParameters(2, "@name", info.Name);
                dbManager.AddParameters(3, "@descr", info.Descr);
                dbManager.AddParameters(4, "@unspsc_code", info.Unspsc_code);
                dbManager.AddParameters(5, "@inc_code", info.Inc_code);
                dbManager.AddParameters(6, "@group_seq", info.InfoGroup.Seq);
                dbManager.AddParameters(7, "@record_status", SystemInfo._WorkRecordStatus);
                dbManager.AddParameters(8, "@created_by", info.InfoRecord.Created_by);
                dbManager.AddParameters(9, "@created_when", info.InfoRecord.Created_when);
                dbManager.AddParameters(10, "@updated_by", info.InfoRecord.Updated_by);
                dbManager.AddParameters(11, "@updated_when", info.InfoRecord.Updated_when);

                strSQL = "UPDATE ICM_MAS_RESOURCE_SUB_GROUP"
                    + " SET ICM_RES_SUB_GRP_CODE = @code"
                    + ", ICM_RES_SUB_GRP_NAME = @name"
                    + ", ICM_RES_SUB_GRP_DESCR = @descr"
                    + ", UNSPSC_CODE = @unspsc_code"
                    + ", INC_CODE = @inc_code"
                    + ", ICM_RES_GRP_SEQ = @group_seq"
                    + ", RECORD_STATUS = @record_status"
                    + ", UPDATED_BY = @updated_by"
                    + ", UPDATED_WHEN = @updated_when"
                    + " WHERE ICM_RES_SUB_GRP_SEQ = @seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Delete(ResourceMasterSubGroupInfo info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@seq", info.Seq);
                dbManager.AddParameters(1, "@record_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@updated_by", info.InfoRecord.Updated_by);
                dbManager.AddParameters(3, "@updated_when", info.InfoRecord.Updated_when);

                strSQL = "UPDATE ICM_MAS_RESOURCE_SUB_GROUP"
                    + " SET RECORD_STATUS = @record_status"
                    + ", UPDATED_BY = @updated_by"
                    + ", UPDATED_WHEN = @updated_when"
                    + " WHERE ICM_RES_SUB_GRP_SEQ = @seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void DeleteAll(IList<ResourceMasterSubGroupInfo> infoList)
        {
            string strSQL;
            string seq = "";
            for (int i = 0; i < infoList.Count; i++)
            {
                if (i == 0)
                {
                    seq += infoList[i].Seq;
                }
                else
                {
                    seq += "," + infoList[i].Seq;
                }
            }

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@record_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@updated_by", infoList[0].InfoRecord.Updated_by);
                dbManager.AddParameters(2, "@updated_when", infoList[0].InfoRecord.Updated_when);

                strSQL = "UPDATE ICM_MAS_RESOURCE_SUB_GROUP"
                    + " SET RECORD_STATUS = @record_status"
                    + ", UPDATED_BY = @updated_by"
                    + ", UPDATED_WHEN = @updated_when"
                    + " WHERE ICM_RES_SUB_GRP_SEQ IN (" + seq + ")";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void DeleteFromDB(IList<ResourceMasterSubGroupInfo> infoList)
        {
            string strSQL;
            string seq = "";
            for (int i = 0; i < infoList.Count; i++)
            {
                if (i == 0)
                {
                    seq += infoList[i].Seq;
                }
                else
                {
                    seq += "," + infoList[i].Seq;
                }
            }

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();

                strSQL = "DELETE FROM ICM_MAS_RESOURCE_SUB_GROUP"
                    + " WHERE ICM_RES_SUB_GRP_SEQ IN (" + seq + ")";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Restore(IList<ResourceMasterSubGroupInfo> infoList)
        {
            string strSQL;
            string seq = "";
            for (int i = 0; i < infoList.Count; i++)
            {
                if (i == 0)
                {
                    seq += infoList[i].Seq;
                }
                else
                {
                    seq += "," + infoList[i].Seq;
                }
            }

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@record_status", SystemInfo._WorkRecordStatus);
                dbManager.AddParameters(1, "@restored_by", infoList[0].InfoRecord.Restored_by);
                dbManager.AddParameters(2, "@restored_when", infoList[0].InfoRecord.Restored_when);

                strSQL = "UPDATE ICM_MAS_RESOURCE_SUB_GROUP"
                    + " SET RECORD_STATUS = @record_status"
                    + ", RESTORED_BY = @restored_by"
                    + ", RESTORED_WHEN = @restored_when"
                    + " WHERE ICM_RES_SUB_GRP_SEQ IN (" + seq + ")";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
